from PySide6.QtWidgets import QLabel, QMenu, QApplication, QWidget, QVBoxLayout
from PySide6.QtGui import QClipboard
from PySide6.QtCore import Qt

class CopyableLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_context_menu)

    def show_context_menu(self, pos):
        context_menu = QMenu(self)
        copy_action = context_menu.addAction("复制")
        copy_action.triggered.connect(self.copy_text)
        context_menu.exec_(self.mapToGlobal(pos))

    def copy_text(self):
        clipboard = QApplication.clipboard()
        clipboard.setText(self.text())

# 示例使用
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    window = QWidget()
    layout = QVBoxLayout(window)
    label = CopyableLabel(window)
    label.setText("这是一个示例文本")
    layout.addWidget(label)
    window.setLayout(layout)
    window.show()
    sys.exit(app.exec_())